package com.financial.service.yspay.service.impl;

import com.financial.service.yspay.dto.FinancialYsPayDTO;
import com.financial.service.yspay.service.FinancialYsPayBuildRequestParaService;
import com.financial.service.yspay.utils.SignUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.Map;

/**
 * @Author: WANGNING
 * @Email wyn20140226@aliyun.com
 * @Date: 2019/6/4 18:24
 * @Des 获取证书路径并且签名接口
 */
@Service
public class FinancialYsPayBuildRequestParaServiceImpl implements FinancialYsPayBuildRequestParaService {

    @Override
    public Map<String,String> buildRequestPara(Map<String,String> sParaTemp, FinancialYsPayDTO financialYsPayDTO) {

        //除去数组中的空值和签名参数
        Map<String,String> sPara = SignUtils.paraFilter(sParaTemp);
        //YsPayDTO sPayDTO=ysBusinessPramService.ysBusinessPram();
        //私钥证书路径
        String partnerCert = financialYsPayDTO.getPATH_PARTER_PKCS12();

        //读取证书
        InputStream pfxCertFileInputStream = null;//servletContext.getResourceAsStream(partnerCert);

        String mysign = "";
        try {
            pfxCertFileInputStream = new ClassPathResource(partnerCert).getInputStream();//加载本地的证书进行https加密传输
            //遍历以及根据重新排序

			/*
			构造静态配置变量（银盛支付配置参数）
			 */
            /*if (SignUtils.ysepayConfig == null) {
                new SignUtils(ysepayConfig);
            }*/
            String signContent = SignUtils.getSignContent(sPara);
            mysign = SignUtils.rsaSign1(signContent, sParaTemp.get("charset"), pfxCertFileInputStream, financialYsPayDTO);

        } catch (Exception e) {
            throw new RuntimeException("签名失败，请检查证书文件是否存在，密码是否正确");
        }

        sPara.put("sign", mysign);
        System.out.println("sPara:"+sPara.toString());
        return sPara;
    }

    //代付验签名
    public Map<String, String> buildRequestParaDF(Map<String, String> sParaTemp, FinancialYsPayDTO financialYsPayDTO) {
        System.out.println("buildRequestParaDF==========");
        //除去数组中的空值和签名参数
        Map<String, String> sPara = SignUtils.paraFilter(sParaTemp);
        //私钥证书路径
        String partnerCert = financialYsPayDTO.getPATH_PARTER_PKCS12_DF();

        System.out.println("buildRequestParaDF====sPara======");

        //读取证书
        InputStream pfxCertFileInputStream = null;//servletContext.getResourceAsStream(partnerCert);
        /*
			构造静态配置变量（银盛支付配置参数）
	    */
        /*if (SignUtils.ysepayConfig == null) {
            new SignUtils(ysepayConfig);
        }*/

        String mysign = "";
        try {
            System.out.println("buildRequestParaDF==========try");
            pfxCertFileInputStream = new ClassPathResource(partnerCert).getInputStream();//加载本地的证书进行https加密传输
            //遍历以及根据重新排序
            String signContent = SignUtils.getSignContent(sPara);

            mysign = SignUtils.rsaSign2(signContent, sParaTemp.get("charset"), pfxCertFileInputStream, financialYsPayDTO);

        } catch (Exception e) {
            System.out.println("签名失败，请检查证书文件是否存在，密码是否正确");
            throw new RuntimeException("签名失败，请检查证书文件是否存在，密码是否正确");
        }

        sPara.put("sign", mysign);

        return sPara;
    }

}
